Component({
  data: {
    selected: 0,
    list: [
      {
        pagePath: "/pages/home/index",
        text: '首页',
        css: 'tab-bar-item'
      },
      {
        pagePath: "/pages/schedule/index",
        text: '日程',
        css: 'tab-bar-item'
      },
      {
        pagePath: "/pages/sport/index",
        css: 'tab-bar-item middle'
      },
      {
        pagePath: "/pages/shopping/index",
        text: '商城',
        css: 'tab-bar-item'
      },
      {
        pagePath: "/pages/profile/index",
        text: '我的',
        css: 'tab-bar-item'
      }
    ]
  },
  pageLifetimes: {
    show: function() {
      // 在组件实例进入页面节点树时执行
      const index = wx.getStorageSync('current-page') || 0;
      this.setData({
        selected: index
      });
    },
  },
  methods: {
    switchTab(e) {
      const path = e.currentTarget.dataset.path;
      wx.switchTab({
        url: path,
      })
      // 更新selected状态
      const index = this.data.list.findIndex(item => item.pagePath === path);
      if (index !== -1) {
        this.setData({
          selected: index
        });
        wx.setStorageSync('current-page', index);
      }
    }
  }
})